package cn.xy.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  判断用户是否登录拦截器
 */
public class LoginIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("=======>方法执行前");
        HttpSession session = request.getSession();

        //判断当前得URL是否是去登录页面的
        if (request.getRequestURI().contains("login")){
            return true;
        }

        //返回是否登录,有没有session
        if (session.getAttribute("name")!=null){
            return true;
        }

        //没有登录就去登录
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);

        return false;
    }
}
